//
//  SettingVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/1.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "SettingVC.h"
#import "SettingContentVC.h"
#import "LoginVM.h"
#import "FeedbackVC.h"
#import "UserNoticeViewController.h"
@interface SettingVC ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headView;
@property (weak, nonatomic) IBOutlet UILabel *labNiceName;
@property (weak, nonatomic) IBOutlet UILabel *labSign;
@property (weak, nonatomic) IBOutlet UILabel *labSex;

@end

@implementation SettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"设置";
    
    self.hidesBottomBarWhenPushed = YES;
    self.tabBarController.tabBar.hidden = YES;
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:0 target:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    self.headView.layer.masksToBounds=YES;
    self.headView.layer.cornerRadius=25.0;
    
    [self updateUI];
}

- (void)updateUI{
    _labNiceName.text=[UserInfoModel share].NickName;
    _labSign.text=[UserInfoModel share].Autograph;
    _labSex.text=[UserInfoModel share].Sex?@"女":@"男";
    [_headView sd_setImageWithURL:[NSURL URLWithString:[UserInfoModel share].Avater]];
}

- (void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- 修改头像
- (void)updateHead{
    UIAlertController *alertCtrl=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancleAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];;
    UIAlertAction *albumAction=[UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        UIImagePickerController * ipc = [[UIImagePickerController alloc] init];
        ipc.delegate = self;
        ipc.allowsEditing=YES;
        ipc.sourceType = sourceType;
        [self presentViewController:ipc animated:YES completion:nil];
    }];
    UIAlertAction *cameraAction=[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        UIImagePickerController * ipc = [[UIImagePickerController alloc] init];
        ipc.delegate = self;
        ipc.allowsEditing=YES;
        ipc.sourceType = sourceType;
        [self presentViewController:ipc animated:YES completion:nil];
    }];
    [alertCtrl addAction:cancleAction];
    [alertCtrl addAction:albumAction];
    [alertCtrl addAction:cameraAction];
    [self presentViewController:alertCtrl animated:YES completion:nil];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [BaseVM manage].viewCtrl=self;
}

#pragma mark -- UINavigationControllerDelegate, UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    self.headView.image=image;
    [[BaseVM manage] postImageWithImage:image andType:2 andSuccess:^(id returnValue) {
        [SVProgressHUD showSuccessWithStatus:@"头像修改成功"];
        [UserInfoModel share].Avater=[returnValue objectForKey:@"Url"];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 9;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf);
    if (indexPath.row==1) {
        ///修改头像
        [self updateHead];
    }else if (indexPath.row==2){
        ///修改昵称
        SettingContentVC *vc=[[SettingContentVC alloc]init];
        vc.title=@"设置昵称";
        vc.strContent=self.labNiceName.text;
        vc.type=SettingTypeNickName;
        vc.backBlock = ^(NSString *str) {
            [weakSelf updateUI];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==3){
        ///修改签名
        SettingContentVC *vc=[[SettingContentVC alloc]init];
        vc.title=@"设置签名";
        vc.strContent=self.labSign.text;
        vc.type=SettingTypeSign;
        vc.backBlock = ^(NSString *str) {
            [weakSelf updateUI];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==4){
        ///修改性别
        SettingContentVC *vc=[[SettingContentVC alloc]init];
        vc.title=@"设置性别";
        vc.sex=(([self.labSex.text isEqualToString:@"男"])?0:1);
        vc.type=SettingTypeSex;
        vc.backBlock = ^(NSString *str) {
            [weakSelf updateUI];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==5){
        ///修改密码
        SettingContentVC *vc=[[SettingContentVC alloc]init];
        vc.title=@"修改密码";
        vc.type=SettingTypePassword;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==6){
        ///关于
        UserNoticeViewController *vc =[[UserNoticeViewController alloc] init];
        
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else if (indexPath.row==8){
        ///退出登录
        [UserModel clean];
        [[LoginVM manage] outLogin];
        [[BaseVM manage] login];
    }else if (indexPath.row==9){
        FeedbackVC *vc=[[UIStoryboard storyboardWithName:@"My" bundle:nil]instantiateViewControllerWithIdentifier:@"FeedbackVC"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
